package stream;

import bean.Student;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lyy
 * @date 2021/12/23
 */
public class MainTest {
    public static void main(String[] args) {
        List<Student> studentList = Arrays.asList(new Student(19,"yyre"),
                                     new Student(2,"ty"),
                                     new Student(13,"ertryr"), new Student(19, "uy"),
                                     new Student(13,"rr"));
        List<String> stringList = Arrays.asList("nulls", "euu", "ioi", "eeeeemoty", "li", "ieu");
        List<Integer> numberList = Arrays.asList(2, 3, 5, 1, 7, 9);

        // map
        List<String> nameList = studentList.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println("map---" + nameList);

        // filter
        List<Student> ageList = studentList.stream().filter(stu -> stu.getAge() > 2).collect(Collectors.toList());
        System.out.println("filter---" + ageList);

        // sorted
        // List<Student> sorted = studentList.stream().sorted(Comparator.comparing(Student::getName)).collect(Collectors.toList());
        List<Student> sortedList = studentList.stream().sorted(Comparator.comparing(Student::getName, Comparator.nullsLast(Comparator.naturalOrder()))).collect(Collectors.toList());
        System.out.println("sorted---" + sortedList);

        // limit
        List<String> limitList = stringList.stream().limit(1).collect(Collectors.toList());
        System.out.println("limit" + limitList);

        // allMatch
        boolean allMatch = stringList.stream().allMatch(String::isEmpty);
        System.out.println("allMatch---" + allMatch);

        // anyMatch
        boolean anyMatch = stringList.stream().anyMatch(Objects::isNull);
        System.out.println("anyMatch---" + anyMatch);

        // max
        Student max = studentList.stream().max(Comparator.comparing(Student::getAge)).get();
        System.out.println("max---" + max);

        // min
        Student min = studentList.stream().min(Comparator.comparing(Student::getAge)).get();
        System.out.println("min---" + min);

        // reduce
        Integer reduce = studentList.stream().map(Student::getAge).reduce((item1, item2) -> item1 + item2).get();
        System.out.println("reduce---" + reduce);

        /**
         * 收集器
         */
        // collect
        List<String> toList = studentList.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println("toList---" + toList);
        LinkedList<String> toLinkedList = studentList.stream().map(Student::getName).collect(Collectors.toCollection(LinkedList::new));
        System.out.println("toLinkedList---" + toLinkedList);
        // toMap要解决键值冲突问题，Duplicate key
        Map<Integer, Student> toMap = studentList.stream().collect(Collectors.toMap(Student::getAge, Function.identity(), (v1, v2)->v2));
        System.out.println("toMap---" + toMap);
        Set<Student> toSet = studentList.stream().collect(Collectors.toSet());
        System.out.println("toSet---" + toSet);
        TreeSet<Student> toTreeSet = studentList.stream().collect(Collectors.toCollection(TreeSet::new));
        System.out.println("toTreeSet---" + toTreeSet);

        // joining
        String joining = stringList.stream().collect(Collectors.joining("<br/>"));
        System.out.println("joining---" + joining);
        String joining2 = stringList.stream().collect(Collectors.joining(",", "[", "]"));
        System.out.println("joining2---" + joining2);

        // partitioningBy
        Map<Boolean, List<String>> partitioningBy = stringList.stream().collect(Collectors.partitioningBy(name -> name.length() > 4));
        System.out.println("partitioningBy---" + partitioningBy);
        // groupingBy
        Map<Integer, List<Student>> groupBy = studentList.stream().collect(Collectors.groupingBy(Student::getAge));
        System.out.println("groupingBy---" + groupBy);
        // 统计分组后的个数
        Map<Integer, Long> counting = studentList.stream().collect(Collectors.groupingBy(Student::getAge, Collectors.counting()));
        System.out.println("counting---" + counting);
        IntSummaryStatistics summaryStatistics = studentList.stream().collect(Collectors.summarizingInt(Student::getAge));
        System.out.println("平均值---" + summaryStatistics.getAverage());
        System.out.println("最大值---" + summaryStatistics.getMax());
        System.out.println("最小值---" + summaryStatistics.getMin());
        System.out.println("总和---" + summaryStatistics.getSum());
        System.out.println("人数---" + summaryStatistics.getCount());
    }
}
